package com.hippo.core.vm

import android.content.Context
import androidx.compose.runtime.mutableStateOf
import androidx.lifecycle.ViewModel
import com.hippo.core.state.SettingState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.setValue
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.viewModelScope
import com.hippo.core.R
import com.hippo.core.config.AppConfig
import com.hippo.core.core.UserManger
import com.hippo.core.event.MessageEvent
import com.hippo.core.event.MessageEventBus
import com.hippo.core.respository.ApiRepository
import com.hippo.core.state.DialogType
import com.hippo.core.utils.UiHandler
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

class SettingVM : ViewModel() {

    var uiState by mutableStateOf(SettingState())

    init {
        UserManger.getUserInfo()?.apply {
            updateState(uiState.copy(userEmail = userEmail, password = password))
        }
    }

    fun dismissDialog() {
        uiState = uiState.copy(dialogType = DialogType.Idle)
    }

    fun showLoading() {
        uiState = uiState.copy(dialogType = DialogType.LoadingDialog())
    }


    fun updateState(state: SettingState) {
        uiState = state
    }


    fun sendEmailCode(email: String, success: (String) -> Unit) {

        uiState = uiState.copy(dialogType = DialogType.LoadingDialog(), newEmail = email)

        viewModelScope.launch {
            ApiRepository.sendEmailCode(email,"3")
                .onFailure {
                    UiHandler.showToast(it.msg)
                    dismissDialog()
                }
                .onSuccess {
                    dismissDialog()
                    UiHandler.showToast("验证码发送成功")
                    success(email)
                }
        }
    }


    fun userOffPC(password:String,callback: () -> Unit) {
        showLoading()
        viewModelScope.launch {
            ApiRepository.userOffPC(password)
                .handleFailure()
                .onSuccess {
                    UserManger.clearUserInfo()
                    dismissDialog()
                    callback()
                }
        }
    }


    fun changeUserEmail(emailCode: String?) {
        if (emailCode.orEmpty().length < 6) {
            UiHandler.showToast(R.string.setting_34)
            return
        }
        showLoading()
        viewModelScope.launch {
            ApiRepository.changeUserEmail(emailCode!!)
                .onFailure {
                    UiHandler.showToast(it.msg)
                    dismissDialog()
                }
                .onSuccess {
                    UserManger.getUserInfo()?.apply {
                        UserManger.updateUserInfo(this.copy(userEmail = uiState.newEmail))
                    }
                    dismissDialog()
                }
        }

    }


    fun deleteAccount(callback: (Boolean) -> Unit) {
        showLoading()
        viewModelScope.launch {
            ApiRepository.userOff()
                .onFailure {
                    dismissDialog()
                    UiHandler.showToast(it.msg)
                }
                .onSuccess {
                    dismissDialog()
                    UserManger.clearUserInfo()
                    callback(true)
                }
        }
    }

    fun logout(callback: () -> Unit) {
        showLoading()
        viewModelScope.launch {
            ApiRepository.loginOut()
                .onFailure {
                    dismissDialog()
                    UiHandler.showToast(it.msg)
                }
                .onSuccess {
                    UserManger.clearUserInfo()
                    dismissDialog()
                    callback()
                }
        }
    }




    fun checkVersion(context: Context) {
        viewModelScope.launch {
            ApiRepository.getLastVersion(AppConfig.VERSION_NAME)
                .onSuccess {
                    if(it?.isUpdate == 1){
                        MessageEventBus.post(MessageEvent.NewVersion(it))
                    }else{
                        UiHandler.showToast(context.getString(R.string.temp_4))
                    }
                }
        }
    }

}